import { Args, Mutation, Parent, Query, ResolveProperty, Resolver } from "@nestjs/graphql";
import { Article } from "src/entities/article.entity";
import { ArticleLabel } from "src/entities/article_label.entity";
import { ArticleLabelInput, ArticleLabelSortInput, ArticleLabelWhereInput, LabelInput, PageInput } from "src/graphql";
import { ArticleLabelService } from "src/services/article-label/article-label.service";
import { ArticleService } from "src/services/article/article.service";
import { LabelService } from "src/services/label/label.service";

@Resolver('ArticleLabel')
export class ArticleLabelResolver {
  constructor(
    private readonly labelService: LabelService,
    private readonly articleService: ArticleService,
    private readonly articleLabelService: ArticleLabelService,
  ) {}

  @Query('article_label')
  async getLabelById(@Args() id: number) {
    return await this.articleLabelService.findOne(id);
  }

  @Query('article_labels')
  async getArticleLabels(@Args('page') page: PageInput,@Args('sort') sort: ArticleLabelSortInput,@Args('where') where: ArticleLabelWhereInput) {
    return await this.articleLabelService.getPagination(page,sort,where);
  }

  @Mutation()
  async addArticleLabel(@Args('article_label') article_label:ArticleLabelInput) {
    return await this.articleLabelService.addOne(<ArticleLabel>article_label);
  }

  @Mutation()
  async removeArticleLabel(@Args() id: number) {
    return await this.articleLabelService.removeOne(id);
  }

  @Mutation()
  async modifyArticleLabel(@Args('id') id: number, @Args('article_label') article_label:ArticleLabelInput) {
    return await this.articleLabelService.modifyOne(id,<ArticleLabel>article_label);
  }

  @ResolveProperty(() => Article,{
    name:'_article'
  })
  async articles(@Parent() article_label){
    const { article_id } = article_label;
    return await this.articleService.findOne(article_id);
  }

  @ResolveProperty(() => Article,{
    name:'_label'
  })
  async labels(@Parent() article_label){
    const { label_id } = article_label;
    return await this.labelService.findOne(label_id);
  }
}